package com.bafa.objects;

import com.bafa.objects.schema.ObjectFactory;
import com.bafa.services.RetrieveServices;

import java.io.Serializable;

import java.util.HashMap;


public class Team implements Serializable{

	private int ID        ;         
	
	private boolean noMatches = false;
	
	private TeamDetails details;
	private TeamStats stats ;
	private int seasonToCheck;
	public Team(com.bafa.objects.schema.Team team){
		details = new TeamDetails(team.getTeamDetails());
		stats = new TeamStats(team.getTeamStats());
		ID = team.getTeamDetails().getID();
	}
	
	public Score[] getMatchList(String seasonId){
		TeamSeason season = stats.getTeamSeason(new Integer(seasonId).intValue());
		return season.getMatches();
	}
	
	public Scorer[] currentScorersArray(){
		TeamSeason season = stats.getTeamSeason(RetrieveServices.getCurrentSeason().getId());
		
		Scorer[] scorers = (Scorer[])season.getScorers().toArray(new Scorer[season.getScorers().size()]);
		
		if(scorers==null)return new Scorer[0];
		return scorers;
	}
	
	public Scorer[] scorersArray(int seasonId){
		TeamSeason season = stats.getTeamSeason(new Integer(seasonId).intValue());
		Scorer[] scorers = (Scorer[])season.getScorers().toArray(new Scorer[season.getScorers().size()]);
		
		if(scorers==null)return new Scorer[0];
		return scorers;
	}
	
	public Scorer[] allTimeScorers(){
		Scorer[] scorers = (Scorer[])stats.getAllTimeScorers().toArray(new Scorer[stats.getAllTimeScorers().size()]);
		
		if(scorers==null)return new Scorer[0];
		return scorers;
		
	}
	public Score[] getCurrentMatchList(){
		
		return getMatchList(new Integer(RetrieveServices.getCurrentSeason().getId()).toString());
	}
	
	
	
	
	
	public String getContactEmail1() {
		if(details.getContactEmail1().indexOf("&apos;")>0)
			return ((String)details.getContactEmail1()).replaceAll("&apos;","'");
		return details.getContactEmail1();
	}
	public String getContactEmail2() {
		if(details.getContactEmail2().indexOf("&apos;")>0)
			return ((String)details.getContactEmail2()).replaceAll("&apos;","'");
		return details.getContactEmail2();
	}
	public String getContactName1() {
		if(details.getContactName1().indexOf("&apos;")>0)
			return ((String)details.getContactName1()).replaceAll("&apos;","'");
		return details.getContactName1();
	}
	public String getContactName2() {
		if(details.getContactName2().indexOf("&apos;")>0)
			return ((String)details.getContactName2()).replaceAll("&apos;","'");
		return details.getContactName2();
	}
	public String getContactTel1() {
		return details.getContactTel1();
	}
	public String getContactTel2() {
		return details.getContactTel2();
	}
	
	public int getID() {
		return ID;
	}
	public String getName() {
		return details.getName();
	}
	
	public String toString(){
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("Team Name: "+getName());
		stringBuffer.append(" & Main Contact: "+getContactName1());
		return stringBuffer.toString();
	}
	
	public boolean equals(Team teams){
		
		return new Integer(this.getID()).equals(new Integer(teams.getID()));
	}

	public String getShirtColour() {
		return details.getShirtColour();
	}

	public boolean isNoMatches() {
		return noMatches;
	}

	public String[] missingKeys(){
		return (String[])getMissingFields().keySet().toArray(new String[getMissingFields().size()]);
	}
	
	public String[] missingValues(){
		return (String[])getMissingFields().values().toArray(new String[getMissingFields().size()]);
	}
	public HashMap getMissingFields() {
		return details.getMissingFields();
	}

	public double getMoneyOwed() {
		return details.getMoneyOwed();
	}

	public boolean isPaymentRequired() {
		return (details.isPaymentRequired());
	}

	public TeamStats getStats() {
		return stats;
	}

	public void setStats(TeamStats stats) {
		this.stats = stats;
	}




	public TeamDetails getDetails() {
		return details;
	}




	public void setDetails(TeamDetails details) {
		this.details = details;
	}
	
	public com.bafa.objects.schema.Team mapTo(){
		ObjectFactory factory = new ObjectFactory();
		com.bafa.objects.schema.Team team = factory.createTeam();
		
		team.setTeamDetails(details.mapTo());
		team.setTeamStats(stats.mapTo());
		return team;
	}

	public int getSeasonToCheck() {
		return seasonToCheck;
	}

	public void setSeasonToCheck(int seasonToCheck) {
		this.seasonToCheck = seasonToCheck;
	}


    public void setID(int iD) {
        this.ID = iD;
    }

    public void setNoMatches(boolean noMatches) {
        this.noMatches = noMatches;
    }
}
